import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const $time 	= $Lazy.file(()=>import('@com/utils/time'))
const ControlModal 		= $Lazy.state(()=>import('./cpt/control-modal'))
// ===================================================================== component
const Prostatus = [
	{ name:'全部', value:'99' },
	{ name:'待审核', value:'0' },
	{ name:'已审核', value:'1' },
	{ name:'待保存', value:'-2' },
	{ name:'待录入', value:'-3' },
]
// ===================================================================== data
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		countData:{},
		// 搜索表单列表
		forms : [
			{ label: '加做项目',		name: 'additional_kind', type:'select', nameStr:'name',data:[{name:'所有',value:'0'},{name:'是',value:'1'}] },
			{ label: '状态',		name: 'enabled',			type: 'select', 	value: '',	data:Prostatus,	onValue:()=> {
				this.setState({selectedKeys:[]})
			}},
			{ label: '标本箱编号',	name: 'box_num_id', type:'select', onValue: ({ rows })=>{
				this.model.box_num = rows ? rows['name'] : ''
			} },
			{ label: '条码号',		name: 'spec_code' },
			{ label: '医院名称',		name: 'hosp_id', type:'select',onValue:({ rows }) => {
				this.model.hosp_name = rows ? rows.name : ''},
			},
			{ label: '录入时间',  	names: ['start_at','end_at'], type:'date-range', name:'jz-time', onValue:()=>this.countData() },
		]
	}
	
	model = {}
	componentDidMount(){
		this.getSelect()
		// 设置时间默认值
		$time.then(f=>{
			const arr = f.duration['10']()
			this.resetModel = { start_at: arr[0], end_at: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			this.fetch()
			this.countData()
		})
		
	}
	// 获取下拉数据
	getSelect = async () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 医院
			$fn.setRow( forms, 'hosp_id', await f.getCache({ api:'hospital' }) )
			//标本箱编号
			$fn.setRow( forms, 'box_num_id', await f.getCache({ api:'box/select3', param:{limit:10000} }) )
			//
			this.setState({ forms })
		})
	}
	// paging
	// 20220304 新增参数 耿琰
	fetch = () => $fn.fetch.call(this,'specimen/itemlists',{isIndex:true,param:{comeSource:'invalid'}})
	
	// 
	countData = ()=>{
		const {start_at, end_at } = this.model
		$http.pull(null,'specimen/count',{ param:{ start_at, end_at}}).then(countData=>{
			this.setState({ countData })
		})
	}
	
	// 表格
	cols = [
		{ title: '标本箱编号',			dataIndex: 'box_num', 			width:100 },
		{ title: '医院名称', 			dataIndex: 'hosp_name', 		width:200 },
		{ title: '条码号', 				dataIndex: 'spec_code', 		...$fn.table.spec_code },
		{ title: '采集时间', 			dataIndex: 'coll_time', 		...$fn.table.time },
		{ title: '项目状态', 			dataIndex: 'enabled', 			width:80, render: text => $fn.getState(Prostatus, text) },
		{ title: '加做项目', 			dataIndex: 'additional_kind', 	width:80, render: text => +text === 1 ? '是' : '否' },
		{ title: '项目列表', 			dataIndex: 'kind_str', 			width:200 },
		{ title: '录入时间', 			dataIndex: 'arrival_time', 		...$fn.table.time },
	]
	// 操作按钮
	controls = () => {
		const { selectedRows } = this.state
		return[
			Controls.click.call(this, '项目作废', ()=> this.modal.open(selectedRows), { isKeys:true })
		]
	}
	
	render(){
		const { countData } = this.state
		return (
			<>
				<Page title='项目作废' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { timeStr:'10' }) }
					{/* 表格 */}
					{ Table.call(this,{type:1,isIndex:true,
						suffix: (
							<>
								<div className='ml10 g9'>审核成功 <b className='f14 c0'>{countData?.audit_pass}</b></div>
								<div className='ml10 g9'>待审核 <b className='f14 c0'>{countData?.audit_audit}</b></div>
								<div className='ml10 g9'>待录入 <b className='f14 c0'>{countData?.audit_add}</b></div>
							</>
						)
					}) }
				</Page>
				{/* 项目作废 */}
				<ControlModal onRef={ref=>this.modal=ref} onRefresh={()=>{
					this.fetch()
					$fn.clearKeys.call(this)
				}} />
			</>
		)
	}
}

export default Index